﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections.Specialized;

namespace Inline.SharePoint.ExtendedVariations.Helpers
{

    class FieldHandler
    {

        public void AddField(StringCollection variationUrls, SPListEventProperties properties)
        {

            for (int i = 0; i < variationUrls.Count; i++)
            {

                using (SPSite site = new SPSite(variationUrls[i]))
                {

                    using (SPWeb web = site.OpenWeb())
                    {

                        SPList targetList = web.Lists.TryGetList(properties.ListTitle);

                        if (targetList == null) // Create the list if not already there on the target
                        {

                            Guid listId = web.Lists.Add(properties.ListTitle, properties.List.Description, properties.List.BaseTemplate);
                            targetList = web.Lists[listId];

                            ListHandler listHandler = new ListHandler(); // Recreate list should it have been deleted
                            listHandler.UpdateSettings(properties.List, targetList); // Sync properties
                            listHandler.UpdateContentTypes(properties.List, targetList); // Sync content types

                        }

                        // Add new field
                        if (!targetList.Fields.ContainsField(properties.FieldName))
                        {

                            targetList.Fields.Add(properties.Field);

                            targetList.ParentWeb.AllowUnsafeUpdates = true;
                            targetList.Update();
                            targetList.ParentWeb.AllowUnsafeUpdates = false;

                        }

                    }

                }

            }

        }

        public void UpdateField(StringCollection variationUrls, SPListEventProperties properties)
        {

            for (int i = 0; i < variationUrls.Count; i++)
            {

                using (SPSite site = new SPSite(variationUrls[i]))
                {

                    using (SPWeb web = site.OpenWeb())
                    {

                        SPList targetList = web.Lists.TryGetList(properties.ListTitle);

                        if (targetList == null) // Create the list if not already there on the target
                        {

                            Guid listId = web.Lists.Add(properties.ListTitle, properties.List.Description, properties.List.BaseTemplate);
                            targetList = web.Lists[listId];

                            ListHandler listHandler = new ListHandler(); // Recreate list should it have been deleted
                            listHandler.UpdateSettings(properties.List, targetList); // Sync properties
                            listHandler.UpdateContentTypes(properties.List, targetList); // Sync content types

                        }

                        if (targetList.Fields.ContainsField(properties.FieldName)) // Update field
                        {

                            SPField field = targetList.Fields.GetFieldByInternalName(properties.FieldName);

                            field.AggregationFunction = properties.Field.AggregationFunction;
                            field.AllowDeletion = properties.Field.AllowDeletion;
                            field.DefaultFormula = properties.Field.DefaultFormula;
                            field.DefaultValue = properties.Field.DefaultValue;
                            field.Description = properties.Field.Description;
                            field.Group = properties.Field.Group;
                            field.Hidden = properties.Field.Hidden;
                            field.Indexed = properties.Field.Indexed;
                            field.NoCrawl = properties.Field.NoCrawl;
                            field.ReadOnlyField = properties.Field.ReadOnlyField;
                            field.RelatedField = properties.Field.RelatedField;
                            field.Required = properties.Field.Required;
                            field.Title = properties.Field.Title;
                            field.Type = properties.Field.Type;

                            field.Update();

                        }
                        else // Add field if for some reason it was deleted
                        {

                            targetList.Fields.Add(properties.Field);

                            targetList.ParentWeb.AllowUnsafeUpdates = true;
                            targetList.Update();
                            targetList.ParentWeb.AllowUnsafeUpdates = false;

                        }

                    }

                }

            }

        }

        public void DeleteField(StringCollection variationUrls, SPListEventProperties properties)
        {

            for (int i = 0; i < variationUrls.Count; i++)
            {

                using (SPSite site = new SPSite(variationUrls[i]))
                {

                    using (SPWeb web = site.OpenWeb())
                    {

                        SPList targetList = web.Lists.TryGetList(properties.ListTitle);

                        if (targetList != null) // If list does not exist at all, the field is already deleted
                        {

                            if (targetList.Fields.ContainsFieldWithStaticName(properties.Field.StaticName))
                            {

                                SPField field = targetList.Fields.GetFieldByInternalName(properties.Field.StaticName);
                                field.Delete();

                                targetList.Fields.Add(properties.Field);

                                targetList.ParentWeb.AllowUnsafeUpdates = true;
                                targetList.Update();
                                targetList.ParentWeb.AllowUnsafeUpdates = false;

                            }

                        }

                    }

                }

            }

        }

    }

}